#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;

namespace PHMEE.EnergyEfficiency;

/// <summary>
/// 燃油流量计的安装情况
/// </summary>
public sealed class FuelFlowMeterConfig
{
    public FlowMeterModeEnum FlowMeterMode { get; set; }

    public List<FlowmeterTemplate> FlowMeterTemplates { get; set; } = [];

    public string? GetFlowMeterName(DeviceFuelEnum device, bool isInlet)
    {
        var template = FlowMeterTemplates.FirstOrDefault(x => x.DeviceType == device && x.IsInlet == isInlet);
        return template?.FlowmeterName ?? null;
    }

    /// <summary>
    /// 是否嵌套安装主机副机总流量计和副机流量计
    /// </summary>
    /// <returns></returns>
    public bool IsMeAeNestedInstallation()
    {
        return FlowMeterMode switch
        {
            FlowMeterModeEnum.Template2 or FlowMeterModeEnum.Template4 => true,
            FlowMeterModeEnum.Template1 or FlowMeterModeEnum.Template3 => false,
            _ => throw new Exception("未知的流量计安装模式")
        };
    }
    
    /// <summary>
    /// 主机油耗是否由流量计监测
    /// </summary>
    /// <returns></returns>
    public bool IsMeBeMonitoredByFlowMeter()
    {
        // 主机单独安装，或者主机副机嵌套，都可以认为通过流量计监测主机油耗
        return FlowMeterTemplates.Any(x => x.DeviceType is DeviceFuelEnum.MainEngine or DeviceFuelEnum.MainAndAssist);
    }
    
    /// <summary>
    /// 副机油耗是否由流量计监测
    /// </summary>
    /// <returns></returns>
    public bool IsAeBeMonitoredByFlowMeter()
    {
        // 副机单独安装，或者主机副机嵌套，都可以认为通过流量计监测副机油耗
        return FlowMeterTemplates.Any(x => x.DeviceType is DeviceFuelEnum.AuxiliaryEngine or DeviceFuelEnum.MainAndAssist);
    }
    
    /// <summary>
    /// 锅炉油耗是否由流量计监测
    /// </summary>
    /// <returns></returns>
    public bool IsBoilerBeMonitoredByFlowMeter()
    {
        return FlowMeterTemplates.Any(x => x.DeviceType == DeviceFuelEnum.Boiler);
    }
}